using System.Collections;
using System;
using Slaks.Graphs.Graphs;

namespace Slaks.Graphs.GraphBuilder
{
	public class GraphBuilderFactory:FactoryBase<GraphBuilderBase>
	{
		private readonly Hashtable _builders;
        private static GraphBuilderFactory s_instance;
        protected GraphBuilderFactory()
        {
            _builders = new Hashtable();
            var builders = GetAvailableAlogrithms();
            foreach (IRepresentation builder in builders)
            {
                if (!builder.GetType().Equals(typeof(GraphBuilderBase)))
                {
                    try
                    {
                        _builders.Add(builder.getBuilderType(), builder);
                    }
                    catch (Exception)
                    {
                        _builders.Add(builder.GetType(), builder);
                    }
                }
            }

        }
		
	    public static GraphBuilderFactory Instance
	    {
	        get
	        {
	            if (s_instance == null)
	            {
	                s_instance = new GraphBuilderFactory();
	            }
	            return (GraphBuilderFactory)s_instance;
	            
	        }
	    }
        
        //public GraphBuilderBase GetGraphBuilder(BuilderType builderType)
        //{
        //    GraphBuilderBase representation;//= null;
        //    representation = (GraphBuilderBase)_builders[builderType];
        //    return representation;
        //}

		public bool AddGraphBuilder(BuilderType builderType, IRepresentation builder)
		{
			if (_builders.Contains(builderType) == false)
			{
				_builders.Add(builder.getBuilderType(), builder);
				return true;
			}
			return false;
		}

		public bool RemoveGraphBuilder(BuilderType builderType)
		{
			if (_builders.Contains(builderType) == false) return false;
			_builders.Remove(builderType);
			return true;
		}

		public bool IsGraphBuilderExists(BuilderType builderType)
		{
			return _builders.Contains(builderType);
		}

	}
}